Commit d3e5540c authored by Kenton Varda's avatar Kenton Varda

Fix bugs in annotations, implement ability to set C++ namespace via annotations.

parent 4d121574
...@@ -84,10 +84,13 @@ libcapnproto_a_SOURCES= \ ...@@ -84,10 +84,13 @@ libcapnproto_a_SOURCES= \
# Tests ============================================================== # Tests ==============================================================
capnpc_inputs = \ capnpc_inputs = \
src/capnproto/c++.capnp \
src/capnproto/test.capnp \ src/capnproto/test.capnp \
src/capnproto/test-import.capnp src/capnproto/test-import.capnp
capnpc_outputs = \ capnpc_outputs = \
src/capnproto/c++.capnp.c++ \
src/capnproto/c++.capnp.h \
src/capnproto/test.capnp.c++ \ src/capnproto/test.capnp.c++ \
src/capnproto/test.capnp.h \ src/capnproto/test.capnp.h \
src/capnproto/test-import.capnp.c++ \ src/capnproto/test-import.capnp.c++ \
......
# Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$id("v3JF2GP4Supe9JSSJ3pnSdUqhJI");
$namespace("capnproto::annotations");
annotation namespace: Text on(file);
...@@ -48,17 +48,26 @@ inline std::ostream& operator<<(std::ostream& os, Void) { ...@@ -48,17 +48,26 @@ inline std::ostream& operator<<(std::ostream& os, Void) {
namespace internal { namespace internal {
void initTestMessage(TestAllTypes::Builder builder); // Explicitly import each of these to make sure they're really located in capnproto::test and not,
void initTestMessage(TestDefaults::Builder builder); // say, the global namespace.
using ::capnproto::test::TestAllTypes;
using ::capnproto::test::TestDefaults;
using ::capnproto::test::TestEnum;
using ::capnproto::test::TestUnion;
using ::capnproto::test::TestUnionDefaults;
using ::capnproto::test::TestNestedTypes;
void checkTestMessage(TestAllTypes::Builder builder); void initTestMessage(test::TestAllTypes::Builder builder);
void checkTestMessage(TestDefaults::Builder builder); void initTestMessage(test::TestDefaults::Builder builder);
void checkTestMessage(TestAllTypes::Reader reader); void checkTestMessage(test::TestAllTypes::Builder builder);
void checkTestMessage(TestDefaults::Reader reader); void checkTestMessage(test::TestDefaults::Builder builder);
void checkTestMessageAllZero(TestAllTypes::Builder builder); void checkTestMessage(test::TestAllTypes::Reader reader);
void checkTestMessageAllZero(TestAllTypes::Reader reader); void checkTestMessage(test::TestDefaults::Reader reader);
void checkTestMessageAllZero(test::TestAllTypes::Builder builder);
void checkTestMessageAllZero(test::TestAllTypes::Reader reader);
} // namespace internal } // namespace internal
} // namespace capnproto } // namespace capnproto
......
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using Cxx = import "c++.capnp";
$Cxx.namespace("capnproto::test");
enum TestEnum { enum TestEnum {
foo @0; foo @0;
bar @1; bar @1;
......
This diff is collapsed.
...@@ -30,7 +30,7 @@ import Data.FileEmbed(embedFile) ...@@ -30,7 +30,7 @@ import Data.FileEmbed(embedFile)
import Data.Word(Word8) import Data.Word(Word8)
import qualified Data.Digest.MD5 as MD5 import qualified Data.Digest.MD5 as MD5
import qualified Data.Map as Map import qualified Data.Map as Map
import Data.Maybe(catMaybes) import Data.Maybe(catMaybes, fromMaybe)
import Data.Binary.IEEE754(floatToWord, doubleToWord) import Data.Binary.IEEE754(floatToWord, doubleToWord)
import Text.Printf(printf) import Text.Printf(printf)
import Text.Hastache import Text.Hastache
...@@ -50,12 +50,20 @@ muNull = MuBool False; ...@@ -50,12 +50,20 @@ muNull = MuBool False;
-- Using a single-element list has the same effect, though. -- Using a single-element list has the same effect, though.
muJust c = MuList [c] muJust c = MuList [c]
namespaceAnnotationId = "v3JF2GP4Supe9JSSJ3pnSdUqhJI.namespace"
fileNamespace desc = fmap testAnnotation $ Map.lookup namespaceAnnotationId $ fileAnnotations desc
testAnnotation (_, TextDesc x) = x
testAnnotation (desc, _) =
error "Annotation was supposed to be text, but wasn't: " ++ annotationName desc
fullName desc = scopePrefix (descParent desc) ++ descName desc fullName desc = scopePrefix (descParent desc) ++ descName desc
scopePrefix (DescFile _) = "" scopePrefix (DescFile _) = ""
scopePrefix desc = fullName desc ++ "::" scopePrefix desc = fullName desc ++ "::"
globalName (DescFile _) = " " -- TODO: namespaces globalName (DescFile desc) = maybe " " (" ::" ++) $ fileNamespace desc
globalName desc = globalName (descParent desc) ++ "::" ++ descName desc globalName desc = globalName (descParent desc) ++ "::" ++ descName desc
-- Flatten the descriptor tree in pre-order, returning struct, union, and interface descriptors -- Flatten the descriptor tree in pre-order, returning struct, union, and interface descriptors
...@@ -303,14 +311,20 @@ importContext parent filename = mkStrContext context where ...@@ -303,14 +311,20 @@ importContext parent filename = mkStrContext context where
context "importIsSystem" = MuBool False context "importIsSystem" = MuBool False
context s = parent s context s = parent s
namespaceContext parent part = mkStrContext context where
context "namespaceName" = MuVariable part
context s = parent s
fileContext desc = mkStrContext context where fileContext desc = mkStrContext context where
flattenedMembers = flattenTypes $ catMaybes $ Map.elems $ fileMemberMap desc flattenedMembers = flattenTypes $ catMaybes $ Map.elems $ fileMemberMap desc
namespace = maybe [] (splitOn "::") $ fileNamespace desc
context "fileName" = MuVariable $ fileName desc context "fileName" = MuVariable $ fileName desc
context "fileBasename" = MuVariable $ takeBaseName $ fileName desc context "fileBasename" = MuVariable $ takeBaseName $ fileName desc
context "fileIncludeGuard" = MuVariable $ context "fileIncludeGuard" = MuVariable $
"CAPNPROTO_INCLUDED_" ++ hashString (fileName desc) "CAPNPROTO_INCLUDED_" ++ hashString (fileName desc ++ ':':fromMaybe "" (fileId desc))
context "fileNamespaces" = MuList [] -- TODO context "fileNamespaces" = MuList $ map (namespaceContext context) namespace
context "fileEnums" = MuList $ map (enumContext context) $ fileEnums desc context "fileEnums" = MuList $ map (enumContext context) $ fileEnums desc
context "fileTypes" = MuList $ map (typeContext context) flattenedMembers context "fileTypes" = MuList $ map (typeContext context) flattenedMembers
context "fileImports" = MuList $ map (importContext context) $ Map.keys $ fileImportMap desc context "fileImports" = MuList $ map (importContext context) $ Map.keys $ fileImportMap desc
......
...@@ -184,9 +184,9 @@ constantDecl = do ...@@ -184,9 +184,9 @@ constantDecl = do
name <- located varIdentifier name <- located varIdentifier
colon colon
typeName <- typeExpression typeName <- typeExpression
annotations <- many annotation
equalsSign equalsSign
value <- located fieldValue value <- located fieldValue
annotations <- many annotation
return (ConstantDecl name typeName annotations value) return (ConstantDecl name typeName annotations value)
typeDecl statements = enumDecl statements typeDecl statements = enumDecl statements
...@@ -235,8 +235,8 @@ fieldDecl = do ...@@ -235,8 +235,8 @@ fieldDecl = do
(name, ordinal) <- nameWithOrdinal (name, ordinal) <- nameWithOrdinal
colon colon
t <- typeExpression t <- typeExpression
annotations <- many annotation
value <- optionMaybe (equalsSign >> located fieldValue) value <- optionMaybe (equalsSign >> located fieldValue)
annotations <- many annotation
return (FieldDecl name ordinal t annotations value) return (FieldDecl name ordinal t annotations value)
negativeFieldValue = liftM (IntegerFieldValue . negate) literalInt negativeFieldValue = liftM (IntegerFieldValue . negate) literalInt
...@@ -288,8 +288,8 @@ paramDecl = do ...@@ -288,8 +288,8 @@ paramDecl = do
name <- varIdentifier name <- varIdentifier
colon colon
t <- typeExpression t <- typeExpression
annotations <- many annotation
value <- optionMaybe (equalsSign >> located fieldValue) value <- optionMaybe (equalsSign >> located fieldValue)
annotations <- many annotation
return (ParamDecl name t annotations value) return (ParamDecl name t annotations value)
annotationDecl = do annotationDecl = do
...@@ -297,10 +297,10 @@ annotationDecl = do ...@@ -297,10 +297,10 @@ annotationDecl = do
name <- located varIdentifier name <- located varIdentifier
colon colon
t <- typeExpression t <- typeExpression
annotations <- many annotation
onKeyword onKeyword
targets <- try (parenthesized asterisk >> return allAnnotationTargets) targets <- try (parenthesized asterisk >> return allAnnotationTargets)
<|> parenthesizedList annotationTarget <|> parenthesizedList annotationTarget
annotations <- many annotation
return (AnnotationDecl name t annotations targets) return (AnnotationDecl name t annotations targets)
allAnnotationTargets = [minBound::AnnotationTarget .. maxBound::AnnotationTarget] allAnnotationTargets = [minBound::AnnotationTarget .. maxBound::AnnotationTarget]
......
This diff is collapsed.
...@@ -24,12 +24,19 @@ ...@@ -24,12 +24,19 @@
module Util where module Util where
import Data.Char (isUpper, toUpper) import Data.Char (isUpper, toUpper)
import Data.List (intercalate) import Data.List (intercalate, isPrefixOf)
--delimit _ [] = "" --delimit _ [] = ""
--delimit delimiter (h:t) = h ++ concatMap (delimiter ++) t --delimit delimiter (h:t) = h ++ concatMap (delimiter ++) t
delimit = intercalate delimit = intercalate
splitOn :: String -> String -> [String]
splitOn _ "" = [""]
splitOn delimiter text | delimiter `isPrefixOf` text =
[]:splitOn delimiter (drop (length delimiter) text)
splitOn delimiter (c:rest) = let (first:more) = splitOn delimiter rest in (c:first):more
-- Splits "camelCase" into ["camel", "Case"]
splitName :: String -> [String] splitName :: String -> [String]
splitName (a:rest@(b:_)) | isUpper b = [a]:splitName rest splitName (a:rest@(b:_)) | isUpper b = [a]:splitName rest
splitName (a:rest) = case splitName rest of splitName (a:rest) = case splitName rest of
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment