src/Structures.cpp
author David Anderson <dvander@alliedmods.net>
Sat Aug 04 17:51:43 2012 -0700 (2012-08-04)
changeset 146 dc7d3b7a918c
parent 92 e3974e7360e9
child 147 9c544823d090
permissions -rw-r--r--
Basic support for parsing and creating types from structure syntax.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include "Structures.h"
    20 #include "Zone.h"
    21 #include "Types.h"
    22 #include "Spaces-inl.h"
    23 #include "Heap-inl.h"
    24 
    25 using namespace ke;
    26 
    27 StructureBufferBuilder::StructureBufferBuilder()
    28   : size_(0)
    29 {
    30 }
    31 
    32 bool
    33 StructureBufferBuilder::add(Handle<Type> type, unsigned *offset)
    34 {
    35     *offset = size_;
    36 
    37     if (!type->isTraceable()) {
    38         // For now, assume the size of primitives is 4-byte.
    39         assert(type->isInt32() || type->isFloat());
    40         size_ += 4;
    41         return true;
    42     }
    43 
    44     if (!offsets_.append(size_))
    45         return false;
    46 
    47     size_ += sizeof(Object *);
    48     return true;
    49 }
    50 
    51 Descriptor *
    52 Descriptor::New(Zone *zone, Handle<String> name, Handle<Type> type, unsigned offset)
    53 {
    54     Local<Descriptor> desc(zone,
    55         Descriptor::cast(zone->allocate(MapKind_Descriptor, sizeof(Descriptor), Heap::Tenure_Old)));
    56     if (!desc)
    57         return NULL;
    58 
    59     desc->name_ = name;
    60     desc->type_ = type;
    61     desc->offset_ = offset;
    62     return desc;
    63 }
    64